package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisManageRightRoleAdminRelConverts;
import com.hunttown.mes.rpc.domain.AnalysisManageRightRoleAdminRelDTO;
import com.hunttown.mes.service.AnalysisManageRightRoleAdminRelService;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisManageRightRoleAdminRelRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisManageRightRoleAdminRelDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageRightRoleAdminRelConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:55
 */
@Service
public class AnalysisManageRightRoleAdminRelRpcServiceImpl implements AnalysisManageRightRoleAdminRelRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageRightRoleAdminRelRpcServiceImpl.class);

    private final AnalysisManageRightRoleAdminRelService analysisManageRightRoleAdminRelService;

    @Autowired
    public AnalysisManageRightRoleAdminRelRpcServiceImpl(AnalysisManageRightRoleAdminRelService analysisManageRightRoleAdminRelService) {
        this.analysisManageRightRoleAdminRelService = analysisManageRightRoleAdminRelService;
    }

    @Override
    public AnalysisManageRightRoleAdminRelDTO insert(AnalysisManageRightRoleAdminRelDTO analysisManageRightRoleAdminRelDTO) {
        try {
            return AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelService.insert(AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel create failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageRightRoleAdminRelDTO analysisManageRightRoleAdminRelDTO) {
        try {
            return analysisManageRightRoleAdminRelService.updateInfoById(AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel update by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageRightRoleAdminRelDTOQuery analysisManageRightRoleAdminRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageRightRoleAdminRelDTOQuery.getW_id() != null && analysisManageRightRoleAdminRelDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageRightRoleAdminRelDTOQuery.getW_id());
            }

            if (analysisManageRightRoleAdminRelDTOQuery.getIds() != null && !analysisManageRightRoleAdminRelDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageRightRoleAdminRelDTOQuery.getIds());
            }

            if (analysisManageRightRoleAdminRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageRightRoleAdminRelDTOQuery.getDeleteFlag());
            }

            if (analysisManageRightRoleAdminRelDTOQuery.getAdminIds() != null && !analysisManageRightRoleAdminRelDTOQuery.getAdminIds().equals("")) {
                query.put("adminIds", analysisManageRightRoleAdminRelDTOQuery.getAdminIds());
            }

            return analysisManageRightRoleAdminRelService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel update by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleAdminRelDTO selectById(Integer id) {
        try {
            return AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel select by id failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageRightRoleAdminRelDTO selectObjByQuery(AnalysisManageRightRoleAdminRelDTOQuery analysisManageRightRoleAdminRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageRightRoleAdminRelDTOQuery.getAdminId() != null && analysisManageRightRoleAdminRelDTOQuery.getAdminId() > 0) {
                query.put("adminId", analysisManageRightRoleAdminRelDTOQuery.getAdminId());
            }

            if (analysisManageRightRoleAdminRelDTOQuery.getRoleId() != null && analysisManageRightRoleAdminRelDTOQuery.getRoleId() > 0) {
                query.put("roleId", analysisManageRightRoleAdminRelDTOQuery.getRoleId());
            }

            if (analysisManageRightRoleAdminRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageRightRoleAdminRelDTOQuery.getDeleteFlag());
            }

            return AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageRightRoleAdminRelDTO> getListForPage(AnalysisManageRightRoleAdminRelDTOQuery analysisManageRightRoleAdminRelDTOQuery) {
        Query query = Query.page(analysisManageRightRoleAdminRelDTOQuery.getPn(), analysisManageRightRoleAdminRelDTOQuery.getPageSize());

        if (analysisManageRightRoleAdminRelDTOQuery.getIds() != null && !analysisManageRightRoleAdminRelDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageRightRoleAdminRelDTOQuery.getIds());
        }

        if (analysisManageRightRoleAdminRelDTOQuery.getLikeName() != null && !analysisManageRightRoleAdminRelDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageRightRoleAdminRelDTOQuery.getLikeName());
        }

        if (analysisManageRightRoleAdminRelDTOQuery.getRoleId() != null) {
            query.put("roleId", analysisManageRightRoleAdminRelDTOQuery.getRoleId());
        }

        if (analysisManageRightRoleAdminRelDTOQuery.getAdminId() != null && analysisManageRightRoleAdminRelDTOQuery.getAdminId() > 0) {
            query.put("adminId", analysisManageRightRoleAdminRelDTOQuery.getAdminId());
        }

        if (analysisManageRightRoleAdminRelDTOQuery.getOrderBy() != null && analysisManageRightRoleAdminRelDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageRightRoleAdminRelDTOQuery.getOrderBy());
        }

        List<AnalysisManageRightRoleAdminRelDTO> list = AnalysisManageRightRoleAdminRelConverts.convert(analysisManageRightRoleAdminRelService.selectByQuery(query));
        Page<AnalysisManageRightRoleAdminRelDTO> page = Page.from(analysisManageRightRoleAdminRelDTOQuery.getPn(), analysisManageRightRoleAdminRelDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageRightRoleAdminRelService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageRightRoleAdminRelService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel delete failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel delete failed, Please order your code.", e);
        }
    }

    @Override
    public void insertForBatch(List<AnalysisManageRightRoleAdminRelDTO> list) {
        try {
            analysisManageRightRoleAdminRelService.insertForBatch(AnalysisManageRightRoleAdminRelConverts.converto(list));
        } catch (Exception e) {
            logger.error("AnalysisManageRightRoleAdminRel insert for batch failed.", e);
            throw new RuntimeException("AnalysisManageRightRoleAdminRel insert for batch failed, Please order your code.", e);
        }
    }
}